package com.jieshuibao.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.config.Global;
import com.jieshuibao.model.db.Category;
import com.jieshuibao.model.db.Drama;
import com.jieshuibao.model.db.DramaExample;
import com.jieshuibao.model.db.DramaExample.Criteria;
import com.jieshuibao.model.db.DramaResourceExample;
import com.jieshuibao.model.db.Teacher;
import com.jieshuibao.service.CategoryService;
import com.jieshuibao.service.DramaResourceService;
import com.jieshuibao.service.DramaService;
import com.jieshuibao.service.TeacherService;
import com.jieshuibao.util.BeanUtils;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.vedio.po.DramaDetailDto;
import com.jieshuibao.vedio.po.DramaDetailReq;
import com.jieshuibao.vedio.po.DramaDetailRes;
import com.jieshuibao.vedio.po.DramaDto;
import com.jieshuibao.vedio.po.DramasReq;
import com.jieshuibao.vedio.po.DramasRes;
import com.jieshuibao.vedio.po.RelationCourseReq;
import com.jieshuibao.vedio.po.RelationCourseRes;

@Controller
@RequestMapping("vedios")
public class DramaController{
	
	
	@Resource
	private DramaService dramaService;
	@Resource
	private DramaResourceService dramaResourceService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private TeacherService teacherService;
	/**
	 * 专辑列表 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="dramas",method=RequestMethod.POST)
	@ResponseBody
	public Object dramas(@RequestBody DramasReq req){
		DramasRes res = new DramasRes();
		DramaExample example = new DramaExample();
		Criteria criteria = example.createCriteria();
		String tags = req.getTag();
		if(StringUtils.isNotEmpty(tags)){
			criteria.andCategoryIdLike("%"+tags+"%");
		}
		example.setOrderByClause("createDate DESC");
		int pageNum = req.getPageNum();
		int pageSize = req.getPageSize();
		example.setPageIndex(pageNum);
		example.setPageSize(pageSize);
		int totalCount = dramaService.countDrama(example);
		List<Drama> dramas = dramaService.getPageDrama(example);
		List<DramaDto> dtos = new ArrayList<DramaDto>();
		for (Drama drama : dramas) {
			DramaResourceExample example2 = new DramaResourceExample();
			com.jieshuibao.model.db.DramaResourceExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andDramaIdEqualTo(drama.getUuid());
			DramaDto dto = new DramaDto();
			int count = dramaResourceService.countDramaResource(example2);
			BeanUtils.copyProperties(dto, drama);
			String categoryId = drama.getCategoryId();
			String tag = getTag(categoryId);
			dto.setTags(tag);
			String teacherId = drama.getTeacherId();
			Teacher teacher = teacherService.getTeacherById(teacherId);
			if(null != teacher){
				dto.setTeacherId(teacherId);
				dto.setDuty(teacher.getDuty());
				dto.setTeacherName(teacher.getName());
			}
			dto.setCount(count);
			dtos.add(dto);
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("totalCount", totalCount);
		map.put("dramas", dtos);
		res.setCode(Global.SUCCESS);
		res.setData(map);
		return res;
	}

	private String getTag(String categoryId) {
		String name = "";
		String tag = "";
		if(categoryId.contains(",")){
			String[] split = categoryId.split(",");
			for (String str : split) {
				Category category = categoryService.getCategoryById(str);
				name = category.getName();
				name+=",";
				tag += name;
			}
		}else{
			Category category = categoryService.getCategoryById(categoryId);
			name = category.getName();
			tag = name;
		}
		if(StringUtils.isEmpty(tag)){
			return "";
		}
		if((tag.charAt(tag.length()-1))==','){
			tag=tag.substring(0,tag.length()-1);
		}
		return tag;
	}
	/**
	 * 课程介绍
	 * @param req
	 * @return
	 */
	@RequestMapping(value="dramaDetail",method=RequestMethod.POST)
	@ResponseBody
	public Object dramaDetail(@RequestBody DramaDetailReq req){
		DramaDetailRes res = new DramaDetailRes();
		String dramaId = req.getDramaId();
		if(StringUtils.isEmpty(dramaId)){
			res.setCode(Global.SYSTEMERROR);
			res.setDes("系统繁忙");
			return res;
		}
		Drama dramaByUuid = dramaService.getDramaByUuid(dramaId);
		String courseIntroduce = dramaByUuid.getCourseIntroduce();
		DramaDetailDto dto = new DramaDetailDto();
		dto.setCourseIntroduce(courseIntroduce);
		res.setData(dto);
		res.setCode(Global.SUCCESS);
		return res;
	}
	/**
	 * 老师相关课程
	 * @param req
	 * @return
	 */
	@RequestMapping(value="relationCourse",method=RequestMethod.POST)
	@ResponseBody
	public Object relationCourse(@RequestBody RelationCourseReq req){
		RelationCourseRes res = new RelationCourseRes();
		String teacherId = req.getTeacherId();
		int pageNum = req.getPageNum();
		int pageSize = req.getPageSize();
		if(StringUtils.isEmpty(teacherId)||StringUtils.isEmpty(teacherId)){
			res.setCode(Global.SYSTEMERROR);
			res.setDes("系统繁忙");
			return res;
		}
		String dramaId = req.getDramaId();
		DramaExample example = new DramaExample();
		Criteria criteria = example.createCriteria();
		criteria.andTeacherIdEqualTo(teacherId);
		criteria.andUuidNotEqualTo(dramaId);
		example.setPageIndex(pageNum);
		example.setPageSize(pageSize);
		int totalCount = dramaService.countDrama(example);
		List<Drama> dramas = dramaService.getPageDrama(example);
		List<DramaDto> dtos = new ArrayList<DramaDto>();
		for (Drama drama : dramas){
			DramaDto dto = new DramaDto();
			BeanUtils.copyProperties(dto, drama);
			dtos.add(dto);
		} 
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("totalCount", totalCount);
		map.put("dramas", dtos);
		res.setCode(Global.SUCCESS);
		res.setData(map);
		return res;
	}
}
